import Header from '../../../../components/Header.vue';
export default {
    name: 'companyadd',
    components: {
        Header
    },
    data() {
        return {
            columns: [
                {
                    label: this.$t('register.companyName'),
                    dataIndex: 'text'
                },
                {
                    label: this.$t('company.resolvingPower'),
                    dataIndex: 'resolution'
                }
            ],
            dataSource: [],
            dataItem: {},
            flag: false,
            loading: true,
            activeName: 'person',
            data4: 'hello',
            name: 'shengwm',
            treeData: [],
            showdialog: false,
            saving: false,
            dataImageList: '',
            addForm: {
                id: null,
                parentId: null,
                companyName: '',
                contact: '',
                phone: '',
                email: '',
                address: '',
                resolution: '',
                totalDuration: ''
            },
            //存放分页数据
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                totalNum: 0
            },
            addRules: {
                title: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 50, message: this.$t('message.length50'), trigger: 'blur'},
                    {pattern: /^([\u4e00-\u9FFFa-zA-Z0-9+\s]+)$/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ],
                contact: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                ],
                phone: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                    {pattern: /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ],
                email: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 50, message: this.$t('message.length50'), trigger: 'blur'},
                    {
                        type: 'email',
                        message: this.$t('login.inputEmail'),
                        trigger: 'blur'
                    }
                ],
                address: [
                    {max: 300, message: this.$t('message.length300'), trigger: 'blur'},
                ],
                resolution: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                ],
                totalDuration: [
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                    {pattern: /^[0-9]+([.]{1}[0-9]+){0,1}$/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ]
            },
            face: {
                name: '',
                group: '',
                file: ''
            },
            defaultProps: {
                children: 'children',
                label: 'text',
                id: 'id'
            },
            groupName: '',
            showdialogroot: false,
            addFormVisible: false,
            formTitle: this.$t('user.newUser'),
            curpid: 0,
            add_sub: false,
            showMain: false,
            groupid: 0,
            titleForm: '',
            //resolutionList: [],
            user: {},
            resolutionTypes: [],
            node: {}
        }
    },
    mounted() {
        this.queryPage(1);
        var data = this.$route.params.data;
        this.curpid = this.$route.params.parentId;
        this.add_sub = this.$route.params.add_sub;
        var resolution = this.$route.params.resolution;
        if (!data && !this.curpid) {
            this.$router.push('/user');
        } else {
            this.flag = this.$route.params.flag;
            if (!!data) {
                this.editNode(data.data);
                this.dataItem = data;
            }
        }

        if (!resolution && !!data && !!data._parent) {
            resolution = data._parent.resolution;
        }

        var user = JSON.parse(localStorage.getItem('userInfo'));
        if (user.companyId != -99 && !!data) {
            resolution = data.resolution;
        }

        var resolutionTypeList = this.$getDictList("device_resolution");
        var resolutionTypes1 = [];
        if (!resolution) {
            this.resolutionTypes = resolutionTypeList;
        } else {
            resolutionTypeList.forEach(function (e) {
                if (parseInt(e.value) <= parseInt(resolution)) {
                    resolutionTypes1.push(e);
                }
            });
            this.resolutionTypes = resolutionTypes1;
        }
    },
    methods: {
        showGroupList() {
            this.axios.hPost("/api/company", {}, (data) => {
                if (data.data) {
                    this.treedata = data.data;
                    this.shownode(data.data[0]);
                }
            });
        },
        //查询
        queryPage: function (pageNum) {
            this.loading = true;
            let params = {
                companyName: this.company_name_query
            };
            let url = '/api/company?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize;
            this.axios.hPost(url, params, (response) => {
                if (!response.success) {
                    this.$message.error(response.msg);
                }
                if (response.data.list) {
                    this.dataSource = response.data.list;
                } else {
                    this.dataSource = response.data;
                }
                this.pageInfo.totalNum = response.data.totalCount;
                this.pageInfo.currentPage = pageNum;
                this.loading = false;
            });
        },

        //保存或修改单位
        saveOrUpdateUser(){
            if (!!this.dataItem.children) {
                if (!!this.addForm.id) {
                    var flag = false;
                    this.dataItem.children.find(item => {
                        if (parseInt(item.resolution) > parseInt(this.addForm.resolution)) {
                            this.$message.error(this.$t('message.resolvingPower'));
                            flag = true;
                            return;
                        }
                    });
                    if (flag) {
                        return;
                    }
                }
            }
            let url = '/api/company/insert';
            if (!!this.addForm.id) {
                url = '/api/company/update';
            }
            this.saving = true;
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    let params = {
                        id: this.addForm.id,
                        companyName: this.addForm.title,
                        contact: this.addForm.contact,
                        phone: this.addForm.phone,
                        email: this.addForm.email,
                        address: this.addForm.address,
                        resolution: this.addForm.resolution,
                        totalDuration: this.addForm.totalDuration,
                        parentId: this.curpid
                    };
                    this.axios.hPost(url, params, (response) => {
                        if (response.success) {
                            this.$message.success(response.msg);
                            this.showdialogroot = false;
                            this.queryPage(1);
                        } else {
                            this.$message.error(response.msg);
                        }
                        this.saving = false;
                        this.$router.push('/user');
                    });
                } else {
                    this.saving = false;
                    return false;
                }
            });
        },
        jumpBack()
        {
            window.history.go(-1);
        },
        editNode(data) {
            this.titleForm = this.$t('company.modifyGroup');
            this.addForm.id = data.id;
            this.addForm.title = data.companyName;
            this.addForm.contact = data.contact;
            this.addForm.phone = data.phone;
            this.addForm.email = data.email;
            this.addForm.address = data.address;
            this.addForm.resolution = data.resolution;
            this.addForm.totalDuration = data.totalDuration + "";
        },
        //人脸识别
        skipFace(){
            this.$router.push({
                path: '/persons',
                name: 'persons',
                params: {
                    companyId: this.addForm.id
                }

            });
        },
        //图像识别
        skipImg(){
            this.$router.push({
                path: '/media',
                name: 'media',
                params: {
                    companyId: this.addForm.id
                }
            });
        },
        //视频会议
        skipMeeting(){
            this.$router.push({
                path: '/videoConference',
                name: 'videoConference',
                params: {
                    companyId: this.addForm.id
                }
            });
        }
    }
}